package com.imut.lagain.repository;

import com.imut.lagain.entity.BreathingExercise;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 呼吸练习数据访问层
 */
@Mapper
public interface BreathingExerciseRepository extends BaseMapper<BreathingExercise> {

    /**
     * 根据用户ID查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY created_at DESC")
    List<BreathingExercise> findByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和会话ID查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND session_id = #{sessionId} AND is_deleted = 0 ORDER BY created_at DESC")
    List<BreathingExercise> findByUserIdAndSessionId(@Param("userId") Long userId, @Param("sessionId") Long sessionId);

    /**
     * 根据用户ID和练习类型查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND exercise_type = #{exerciseType} AND is_deleted = 0 ORDER BY created_at DESC")
    List<BreathingExercise> findByUserIdAndExerciseType(@Param("userId") Long userId, @Param("exerciseType") String exerciseType);

    /**
     * 根据用户ID和状态查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND status = #{status} AND is_deleted = 0 ORDER BY created_at DESC")
    List<BreathingExercise> findByUserIdAndStatus(@Param("userId") Long userId, @Param("status") String status);

    /**
     * 根据用户ID和日期范围查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0 ORDER BY created_at DESC")
    List<BreathingExercise> findByUserIdAndDateRange(@Param("userId") Long userId, 
                                                     @Param("startDate") LocalDateTime startDate, 
                                                     @Param("endDate") LocalDateTime endDate);

    /**
     * 根据练习ID查询呼吸练习记录
     */
    @Select("SELECT * FROM breathing_exercise WHERE id = #{exerciseId} AND is_deleted = 0")
    BreathingExercise findByExerciseId(@Param("exerciseId") Long exerciseId);

    /**
     * 统计用户练习次数
     */
    @Select("SELECT COUNT(*) FROM breathing_exercise WHERE user_id = #{userId} AND is_deleted = 0")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 统计用户在指定日期范围内的练习次数
     */
    @Select("SELECT COUNT(*) FROM breathing_exercise WHERE user_id = #{userId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0")
    Long countByUserIdAndDateRange(@Param("userId") Long userId, 
                                   @Param("startDate") LocalDateTime startDate, 
                                   @Param("endDate") LocalDateTime endDate);

    /**
     * 统计用户不同练习类型的次数
     */
    @Select("SELECT exercise_type, COUNT(*) as count FROM breathing_exercise WHERE user_id = #{userId} AND is_deleted = 0 GROUP BY exercise_type")
    List<Map<String, Object>> countByUserIdGroupByExerciseType(@Param("userId") Long userId);

    /**
     * 查询用户最近的呼吸练习
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY created_at DESC LIMIT 1")
    BreathingExercise findLatestByUserId(@Param("userId") Long userId);

    /**
     * 查询用户最有效的呼吸练习（根据效果评分）
     */
    @Select("SELECT * FROM breathing_exercise WHERE user_id = #{userId} AND effectiveness_rating IS NOT NULL AND is_deleted = 0 ORDER BY effectiveness_rating DESC LIMIT 1")
    BreathingExercise findMostEffectiveByUserId(@Param("userId") Long userId);

    /**
     * 更新呼吸练习状态
     */
    @Update("UPDATE breathing_exercise SET status = #{status}, updated_at = NOW() WHERE id = #{id} AND is_deleted = 0")
    int updateStatus(@Param("id") Long id, @Param("status") String status);

    /**
     * 更新呼吸练习效果评分
     */
    @Update("UPDATE breathing_exercise SET effectiveness_rating = #{rating}, anxiety_before = #{anxietyBefore}, anxiety_after = #{anxietyAfter}, updated_at = NOW() WHERE id = #{id} AND is_deleted = 0")
    int updateEffectiveness(@Param("id") Long id, 
                           @Param("rating") Integer rating, 
                           @Param("anxietyBefore") Integer anxietyBefore, 
                           @Param("anxietyAfter") Integer anxietyAfter);

    /**
     * 软删除呼吸练习记录
     */
    @Update("UPDATE breathing_exercise SET is_deleted = 1, deleted_at = NOW() WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 分页查询用户呼吸练习记录
     */
    IPage<BreathingExercise> selectPageByUserId(Page<BreathingExercise> page, @Param("userId") Long userId);
}